import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/models/odoo/product_equhisdata.dart';
import 'package:flutter_admin/pages/common/date_time_select.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import '../../../generated/l10n.dart';

class ProductEquhisdataDialog extends StatefulWidget {
  ProductEquhisdataDialog();

  @override
  _ProductEquhisdataDialogState createState() => _ProductEquhisdataDialogState();
}

class _ProductEquhisdataDialogState extends State<ProductEquhisdataDialog> {

  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  ProductEquhisdata  productEquhisdata = ProductEquhisdata();

   //设备名称
  List<SelectOptionVO> devList = [];
   //参数名称
  List<SelectOptionVO> unitnameList = [];
   //采集时间
  List<SelectOptionVO> histimeList = [];

  DateFormat format = DateFormat('yyyy-MM-dd HH:mm:ss');

  @override
  void initState() {
    productEquhisdata.histimeStart = Utils.getDate()+" 00:00:00";
    productEquhisdata.histimeEnd = Utils.getDate()+" 23:59:59";
    super.initState();
    _loadData();
  }

  _loadData() async{
    //设备名称
    devList = await DynamicDataApi.getModelSelect(
        model: 'base.productionstation',
        tag: 'epu_id',
        name: 'stationname',
        key: 'isproduction',
        val: '1'
    );
    setState(() { });
  }
  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          DateTimeSelect(
            context,
            label: '采集开始日期',
            value: productEquhisdata.histimeStart,
            lastDate: productEquhisdata.histimeEnd!=''&&productEquhisdata.histimeEnd!=null?format.parse(productEquhisdata.histimeEnd!):null,
            onDateChange: (v) {
              formKey.currentState?.save();
              productEquhisdata.histimeStart = v;
              setState(() {});
            },
            onSaved: (v) {
              productEquhisdata.histimeStart = v;
            },
          ),
          DateTimeSelect(
            context,
            label: '采集结束时间',
            value: productEquhisdata.histimeEnd,
            firstDate: productEquhisdata.histimeStart!=''&&productEquhisdata.histimeStart!=null?format.parse(productEquhisdata.histimeStart!):null,
            onDateChange: (v) {
              formKey.currentState?.save();
              productEquhisdata.histimeEnd = v;
              setState(() {});
            },
            onSaved: (v) {
              productEquhisdata.histimeEnd = v;
            },
          ),
          CrySelect(
            label: '设备名称',
            dataList: devList,
            value: productEquhisdata.devid,
            onChange: (v) async {
              if (productEquhisdata.devid != v) {
                formKey.currentState!.save();
                productEquhisdata.devid = v;
                productEquhisdata.unitname = null;
                try {
                  unitnameList = await DynamicDataApi.getModelSelect(
                      model: "product.equparameter",
                      tag: "unitname",
                      name: "unitname",
                      key: 'devid',
                      val: v,
                      active:false
                  );
                } catch (e) {
                  unitnameList = [];
                }
                setState(() {});
              }
            },
            onSaved: (v) => { productEquhisdata.devid = v},
          ),
          CrySelect(
            label: '参数名称',
            dataList: unitnameList,
            value: productEquhisdata.unitname,
            onSaved: (v) => { productEquhisdata.unitname = v},
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, productEquhisdata);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}